//
//  SubmitTicketInfoView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/23.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class SubmitTicketInfoView: UIView {
    
    // 金额
    private lazy var moneyLabel = UILabel(title: "¥0", color: COLOR_NICE,size: 18)
    // 实付金额
    let moneyTitile = UILabel(title: "实付金额",color: UIColor.gray,size: 12)
    // 打折金额
    private let moneyFake = LabelDeleteLine(title: "¥1387",color: UIColor.gray,size: 14)
    // 提交按钮
    public lazy var submitBtn: UIButton = {
        let ui = UIButton()
        ui.backgroundColor = COLOR_RED
        ui.setTitle("去支付", for: .normal)
        ui.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        return ui
    }()
    
    
    // 设置金额
    public var money:String? {
        didSet {
            guard let mon = money else {
                return
            }
            if mon.elementsEqual("0") {
                moneyLabel.text = "¥0"
                moneyFake.isHidden = true
            }
            moneyLabel.text = "¥\(mon)"
            //  会员
            let permissionId = UserDefaults.standard.integer(forKey: SP_PERMISSIONID)
            if  permissionId == 2 ,!mon.elementsEqual("0"){
                moneyFake.text = "¥\(mon)"
                moneyFake.isHidden = false
                moneyTitile.text = "会员折扣价"
                moneyLabel.text = "¥\(Int(Double(mon)! * 0.88))"
            }
        }
    }
    
    // 修改支付文本
    public var submitBtnText:String? {
        didSet {
            submitBtn.setTitle(submitBtnText, for: .normal)
        }
    }
    
    // 回调
    public var complte: (() -> Void)?
    
    override func layoutSubviews() {
        // 实付金额
        moneyTitile.frame = CGRect(x: MARGIN, y: (frame.height - MARGIN50) * 0.5, width: MARGIN100, height: MARGIN)
        addSubview(moneyTitile)
        // 金额
        moneyLabel.textColor = COLOR_RED
        let moneyY = moneyTitile.frame.maxY + MARGIN5
        moneyLabel.frame = CGRect(x: MARGIN, y: moneyY, width: MARGIN140, height: MARGIN20)
        addSubview(moneyLabel)
        // 打折金额
        moneyFake.frame = CGRect(x: moneyLabel.frame.maxY + MARGIN25, y: moneyY, width: MARGIN100, height: MARGIN20)
        moneyFake.isHidden = true
        addSubview(moneyFake)
        // 提交按钮
        submitBtn.frame = CGRect(x: frame.width - 120, y: 0, width: 120, height: frame.height)
        addSubview(submitBtn)
        // 提交按钮 添加点击事件
        submitBtn.addTarget(self, action: #selector(clickItem), for: .touchUpInside)
    }
    
    @objc func clickItem() -> Void {
        if let callback = complte {
            callback()
        }
    }
}
